package net.kts.story.map;

import net.kts.location.MyLocationManager;
import net.kts.main.R;
import net.kts.story.StoryObjectHouse;
import net.kts.story.map.overlay.StorySelectMarkerOveray;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class StoryMap {
	private Activity          activity;
	private final MapView     mapView;
	private StorySelectMarkerOveray myMarkerOveray;
	
	
	public StoryMap(StoryObjectHouse storyObjectHouse, MapView mapView) {
		super();
		this.activity = storyObjectHouse.getMainActivity();
		this.mapView = mapView;
		
		createStoryListener(storyObjectHouse);
		
		Drawable drawable = activity.getResources().getDrawable(R.drawable.story_flag_my);
		myMarkerOveray = new StorySelectMarkerOveray(drawable);
		
		this.mapView.getOverlays().add(myMarkerOveray);
		this.mapView.getOverlays().add(new StoryTouchListener(drawable, storyObjectHouse));
	
		
		refreshMyLocation();
	}


	private void createStoryListener(StoryObjectHouse storyObjectHouse) {
		StoryMapLocationListener storyLocationListener = new StoryMapLocationListener(storyObjectHouse);
		MyLocationManager.addLocationListener(storyLocationListener);
	}

	
	public Location refreshGeoLocation() {
		return MyLocationManager.giveMeMyCurrentLocation();
	}

	private void refreshMyLocation() {
			Location location = MyLocationManager.giveMeMyCurrentLocation();
			
			Double geoLat = location.getLatitude() * 1000000;
			Double geoLng = location.getLongitude() * 1000000;
			GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
			
			OverlayItem overlayItem = new OverlayItem(
					point, 
					"", 
					"");
			myMarkerOveray.updateOverlay(overlayItem);
			mapView.getOverlays().add(0, myMarkerOveray);
	}
	
	public void onResume() {
		refreshMyLocation();
	}
	
//	class HiThereHandler extends Handler {
//		private double lat = 0;
//		private double lng = 0;
//		public HiThereHandler(double lat, double lng) {
//			super();
//			this.lat = lat;
//			this.lng = lng;
//		}
//
//		@Override
//		public void handleMessage(Message msg) {
//			super.handleMessage(msg);
//			hithere.surroundTwitter(lat * 0.000001, lng * 0.000001);
//		}
//	}	
}
